import { loginApi, getUserApi } from '@/api/user.js'
import { getToken, setToken, delToken } from '@/utils/storage.js'
export default {
	state() {
		return {
			token: getToken(),
			userInfo: {}
		}
	},
	mutations: {
		changeToken(state, token) {
			state.token = token
			setToken(token)
		},
		resetToken(state) {
			state.token = null
			delToken()
		},
		setUser(state, userInfo) {
			state.userInfo = userInfo
		}
	},
	actions: {
		async asyncLogin({ commit }, ruleForm) {
			const res = await loginApi(ruleForm)
			commit('changeToken', res.data.token)
			const res2 = await getUserApi(ruleForm)
			commit('setUser', res2.data)
		}
	},
	getters: {},
	namespaced: true
}